﻿@model BillBox.Models.Payment

@{
    ViewBag.Title = "New Payment";
    Layout = "~/Views/Shared/_Layout.cshtml";

    var paymentMethods = ViewBag.paymentMethods as IEnumerable<BillBox.Models.PaymentMethod>;
    var bill = ViewBag.bill as BillBox.Models.Bill;
    int paymentsCount = bill != null ? bill.Payments.Count : 0;
    string formDisplay = paymentsCount > 0 ? "none" : "block";
}

<div class="row main">
    <div class="col-lg-12">
        <ol class="breadcrumb">
            <li>@Html.ActionLink("Home", "Index", "Default")</li>
            <li><a href="#">Payment</a></li>
            <li class="active">New Payment</li>
        </ol>
        <!--<h1 class="page-header">New Payment</h1>-->
        @if(TempData["Message"] != null)
        {
            <div class="alert alert-success">@TempData["Message"]</div>
        }
        else if (TempData["Error"] != null)
        {
            <div class="alert alert-danger">@TempData["Error"]</div>
        }
        <div class="panel panel-default">
            <div class="panel-heading">
                New Payment
            </div>
            <div class="panel-body">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        Bill Information
                    </div>
                    <div class="panel-body">
                        @if (bill != null)
                        {	
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>@Html.DisplayNameFor(p => p.Bill.Subscriber)</th>
                                            <th>@Html.DisplayNameFor(p => p.Bill.BillId)</th>
                                            <th>@Html.DisplayNameFor(p => p.Bill.Date)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>@bill.Subscriber.Name</td>
                                            <td>@bill.BillId</td>
                                            <td>@bill.Date</td>
                                        </tr>
                                    </tbody>

                                </table>
                            </div>
                        }
                    </div>
                </div>
                @if (paymentsCount > 0)
                {
                    <div class="panel panel-default">
                        <div class="panel-heading">Payments</div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>@Html.DisplayNameFor(p => p.PaymentMethodId)</th>
                                            <th>Data</th>
                                            <th>@Html.DisplayNameFor(p => p.Amount)</th>
                                            <th>&nbsp;</th>
                                        </tr>
                                    </thead>
                                    <tbody>

                                        @foreach (BillBox.Models.Payment payment in bill.Payments)
                                        {
                                            <tr>
                                                <td>@payment.PaymentMethod.Name</td>

                                                
                                                <td>
                                                    @foreach (BillBox.Models.PaymentPaymentMethodCaptureField field in payment.PaymentPaymentMethodCaptureFields)
                                                    {
                                                        <span>@field.PaymentMethodCaptureField.DisplayName</span>
                                                        <span>:&nbsp;</span>
                                                        <span>@field.Value</span>
                                                        <br />
                                                    }
                                                </td>
                                                <td>@BillBox.Common.Util.DisplayForDollar(payment.Amount)</td>
                                                <td>@Html.ActionLink("Remove", "RemovePayment", new { paymentId = payment.PaymentId })</td>
                                            </tr>
                                        }
                                        <tr>
                                            
                                            <td>&nbsp;</td>
                                            <td>&nbsp;</td>
                                            <td><strong>@BillBox.Common.Util.DisplayForDollar(bill.Total())</strong></td>
                                            <td>&nbsp;</td>
                                        </tr>
                                    </tbody>

                                </table>
                            </div>
                        </div>
                    </div>

                }

                <div class="rowsdsds">
                    <div class="form-container" id="payment-form-container" style="display: @formDisplay">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                Add Payment
                            </div>
                            <div class="panel-body">
                                <div class="col-lg-6">
                                    @using (Html.BeginForm("NewPayment", "Payment", FormMethod.Post, new { role = "form", @class = "form-horizontal" }))
                                    {
                                        @Html.AntiForgeryToken()
                                        @Html.ValidationSummary(true)
                                        @Html.HiddenFor(model => model.BillId)

                                        <div class="form-group">
                                            @Html.LabelFor(model => model.PaymentMethodId, new { @class = "col-sm-5 control-label" })
                                            <div class="col-sm-7">
                                                @Html.DropDownListFor(model => model.PaymentMethodId, new SelectList(paymentMethods, "PaymentMethodId", "Name"), new { @class = "form-control" })
                                                @Html.ValidationMessageFor(model => model.PaymentMethodId)
                                            </div>
                                        </div>
                                        <div class="form-group" id="amount-field" style="display: none">
                                            @Html.LabelFor(model => model.Amount, new { @class = "col-sm-5 control-label" })
                                            <div class="col-sm-7">
                                                @Html.TextBoxFor(model => model.Amount, new { @class = "form-control" })
                                                @Html.ValidationMessageFor(model => model.Amount)
                                            </div>
                                        </div>

                                        <div id="capture-fields-area"></div>
                                        <div class="form-group">
                                            <div class="col-sm-offset-5 col-sm-7">
                                                <button type="submit" class="btn btn-default">Add</button>
                                                <button type="reset" class="btn btn-default" id="cancel-new-payment">Cancel</button>
                                            </div>
                                        </div>

                                    }
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="panel-footer">
                @if (paymentsCount > 0)
                {

                    <ul class="action-menu">
                        <li>@Html.ActionLink("Post", "PostBill", new { billId = bill.BillId }, new { @class = "btn btn-success" })</li>
                        <li><a href="#" id="add-payment-link" class="btn btn-default">Add Payment</a></li>
                    </ul>

                }
            </div>
        </div>
    </div>
</div>



@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
        $(document).ready(function () {

            loadCaptureFields($('#@Html.IdFor(model => model.PaymentMethodId)').val());

            $('#@Html.IdFor(model => model.PaymentMethodId)').change(function (e) {
                loadCaptureFields($(this).val());
            });

            $("#add-payment-link").on("click", function (e) {
                e.preventDefault();
                $("#payment-form-container").show();
                $("#bill-action-menu").hide();
            });

            $("#cancel-new-payment").on("click", function (e) {
                e.preventDefault();
                $("#payment-form-container").hide();
                $("#bill-action-menu").show();
            });
        });

        function loadCaptureFields(paymentMethodId) {
            if (paymentMethodId) {
                var captureFields = "";

                $.ajax({
                    cache: false,
                    type: 'GET',
                    url: '@Url.Action("GetPaymentMethodCaptureFields", "Payment")',
                    data: { 'paymentMethodId': paymentMethodId },
                    success: function (html) {
                        $("#capture-fields-area").html(html);
                        $("#amount-field").show();
                    },
                    error: function () {
                        alert("Error");
                    }
                });
            } else {
                $("#capture-fields-area").html("");
            }
        }
    </script>
}
